import os
import yaml
import uvicorn
from loguru import logger
from pathlib import Path
from script.web.app import app

def setup_logging():
    """配置日志系统"""
    log_dir = Path("logs")
    log_dir.mkdir(exist_ok=True)
    
    logger.add(
        "logs/app.log",
        rotation="500 MB",
        retention="10 days",
        level="INFO",
        format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}"
    )

def load_config():
    """加载配置文件"""
    with open("config.yml", 'r', encoding='utf-8') as f:
        return yaml.safe_load(f)

def main():
    """主程序入口"""
    # 设置日志
    setup_logging()
    logger.info("Starting CSA Anti-Poison Scanning Engine...")
    
    # 加载配置
    config = load_config()
    
    # 创建必要的目录
    for dir_name in ['data_dir', 'temp_dir', 'cache_dir']:
        Path(config['storage'][dir_name]).mkdir(parents=True, exist_ok=True)
    
    # 启动Web服务
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=8000,
        log_level="info"
    )

if __name__ == "__main__":
    main()
